package com.example.evanproject.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.evanproject.domain.Chatlist;
import com.example.evanproject.domain.Register;
import com.example.evanproject.mapper.RegisterMapper;
import com.example.evanproject.service.ChatlistService;
import com.example.evanproject.mapper.ChatlistMapper;
import com.example.evanproject.util.EntityMapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
* @author hanxiaoqi
* @description 针对表【chatlist】的数据库操作Service实现
* @createDate 2024-03-09 21:52:12
*/
@Service
public class ChatlistServiceImpl extends ServiceImpl<ChatlistMapper, Chatlist>
    implements ChatlistService{

    @Autowired
    private ChatlistMapper chatlistMapper;
    @Autowired
    private RegisterMapper registerMapper;
    @Override
    public List<Map> selectbyidlist(Integer userid) {

//        这是筛选出来的信息聊天列表(可以通过筛选出来的将好友的信息放到里面)
            List<Chatlist> list= chatlistMapper.selectbyidwherelastinfo(userid);
            System.out.println(list);
            List<Map> list2=new ArrayList<>();
            for(int i=0;i<list.size();i++){
                Map<String,Object> polist= EntityMapUtils.entityToMap(list.get(i));
                if(list.get(i).getSendId()!=userid){
                    polist.put("friendsinfo", trunlist(list.get(i).getSendId()));
                }
                if(list.get(i).getToId()!=userid){
                    polist.put("friendsinfo",trunlist(list.get(i).getToId()));
                }
                list2.add(polist);
            }
            return list2;
        }

    @Override
    public List<Map> selectchatrecordsbytwoid(Integer userid1, Integer userid2) {
        List<Chatlist> chatlist=registerMapper.selectchatrecordsbytwoid(userid1,userid2);
        List<Map> list2=new ArrayList<>();
        for (int i = 0; i < chatlist.size(); i++) {
            Map<String,Object> polist= EntityMapUtils.entityToMap(chatlist.get(i));
            polist.put("sendinfo", trunlist(chatlist.get(i).getSendId()));
            polist.put("toinfo",trunlist(chatlist.get(i).getToId()));
            list2.add(polist);
        }
        return list2;
    }

    @Override
    public Integer sendlist(Integer sendid, Integer toid, String message) {
        //获取当前时间带有时分秒
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        String time= dateFormat.format(date);
        return chatlistMapper.sendlist(sendid,toid,message,time);
    }

    public Map trunlist(Integer id){
        Register register= registerMapper.selectById(id);
        Map<String,Object> maplist=new HashMap<>();
        maplist.put("name",register.getSName());
        maplist.put("userid",register.getSId());
        maplist.put("img",register.getSImg());
        return maplist;
    }

}




